package com.tchhy.mvplibrary.ui.activity

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.provider.Settings
import com.tbruyelle.rxpermissions2.RxPermissions
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import java.lang.reflect.AccessibleObject.setAccessible
import androidx.core.os.HandlerCompat.postDelayed

/**
 * Author: wfy
 *
 * time: 2018/11/22
 *
 */
open class PermissionActivity : RxAppCompatActivity() {
    private var mRxPermissions: RxPermissions? = null
    private val CODE_REQUEST_PERMISSIONS: Int = 3000

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mRxPermissions = RxPermissions(this)
    }

    open fun jumpToSplashActivity() {
        try {
            var clz = Class.forName("com.tchhy.tcjk.ui.splash.SplashActivity")
            startActivity(Intent(this, clz))
            finish()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mRxPermissions = null

    }


    @SuppressLint("CheckResult")
    fun requestPermission(vararg requests: String, success: () -> Unit) {
        mRxPermissions
                ?.requestEachCombined(*requests)
                ?.subscribe { permission ->
                    if (permission.granted) {
                        success.invoke()
                    } else if (!permission.shouldShowRequestPermissionRationale) {

                        var str = ""
                        when(permission.name){
                            "android.permission.CAMERA"->{
                                str = "相机"
                            }
                            "android.permission.RECORD_AUDIO"->{
                                str = "录音"
                            }
                            "android.permission.ACCESS_FINE_LOCATION"->{
                                str = "定位"
                            }
                            "android.permission.WRITE_EXTERNAL_STORAGE"->{
                                str = "写入存储"
                            }
                            "android.permission.READ_PHONE_STATE"->{
                                str = "获取手机信息"
                            }
                            "android.permission.READ_CONTACTS" -> {
                                str = "读取联系人"
                            }

                        }
                        showDialog(str)
                    }
                }
    }

    private fun showDialog(name:String) {
        AlertDialog.Builder(this).setTitle("提示")
                .setMessage("您拒绝了${name}权限，可能会造成部分功能无法正常使用，是否现在去授予权限？")
                .setPositiveButton("去授权") { dialog, _ ->
                    dialog.dismiss()
                    goGrantedPermission()
                }
                .setNegativeButton("取消", null)
                .show()
    }

    /*
       引导用户至设置页手动授权
   */
    public fun goGrantedPermission() {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.fromParts("package", packageName, null)
        startActivityForResult(intent, CODE_REQUEST_PERMISSIONS)
    }

}